//引入第三方express模块
const express=require("express");
//引入第三方路由器模块
const router=express.Router();
//引入连接池
const pool=require("../pool")

//创建路由，端口
router.get("/",(req,res)=>{
  var sql="select * from useraccount"
  pool.query(sql,[],(err,result)=>{
    if(err) console.log(err);
    res.send(result)
  })
})

router.get("/getinformation",(req,res)=>{
  if(!req.session.uid){
    res.send({code:0,msg:"请先登录"})
    return;
  }
  var uid=req.session.uid
  var sql="select * from useraccount where uid=?"
  pool.query(sql,[uid],(err,result)=>{
    if(err) console.log(err);
    //console.log(result)
    if(result.length>0){
      res.send({code:1,msg:result})
    }else{
      res.send({code:-1})
    } 
  })
})

router.post('/update',(req,res)=>{
  if(!req.session.uid){
    res.send({code:0,msg:"请先登录"})
    return;
  }
  var uid=req.session.uid
  console.log(req.body.updatedAccount);
  var updatedAccount=parseFloat(req.body.updatedAccount); 
  var sql="update useraccount set useraccount=? where uid=?"
  pool.query(sql,[updatedAccount,uid],(err,result)=>{
    //console.log("111")
    if(err) throw err;
    //console.log(result);
    if(result.affectedRows>0){
      res.send("200")
    }
  })
})
//公开模块
module.exports=router;


